class Place < ActiveRecord::Base

  has_one :place
  belongs_to :user
  belongs_to :country

  before_create :set_nicename

  validates_presence_of :user_id
  validates_presence_of :name
  
  def get_name
    self.name.blank? ? 'lugar sin nombre' : self.name
  end
  
  def clone_place(user)
    user.places
    place = self.clo
  end
  
  
  private
  
  def set_nicename
    original_name = self.name
    return '' if original_name.blank?
    ret = original_name.sanitize
    while user.places.find(:first, :conditions =>[ "nicename = ?", ret ]) do
      suffix ||= 1
      suffix += 1
      ret = original_name.sanitize + "-#{suffix}"
    end
    self.nicename = ret
  end
  
  
end
  